package com.view.log_inMenu;

import com.run.UserRun;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Objects;

public class register {
    private JLabel info = new JLabel("请输入账号信息");
    private JLabel usernameLabel = new JLabel("账号:");
    private JLabel passwordLabel = new JLabel("密码:");
    private JLabel IDLabeel = new JLabel("ID:");
    private JTextField username = new JTextField();
    private JTextField password = new JTextField();
    private JTextField ID = new JTextField("管理员无需填写");
    private JButton button = new JButton("确定");
    private Box mainBox = Box.createVerticalBox();
    private Box usernameBox = Box.createHorizontalBox();
    private Box passwordBox = Box.createHorizontalBox();
    private Box IDBox = Box.createHorizontalBox();
    private JPanel mainPanel = new JPanel();
    private JFrame jFrame = new JFrame("注册");
    private JRadioButton identityButton1 = new JRadioButton("学生",true);
    private JRadioButton identityButton2 = new JRadioButton("管理员");
    private ButtonGroup identityGroup = new ButtonGroup();
    private Box identityBox = Box.createHorizontalBox();
    private JLabel identityLabel = new JLabel("身份:");

    public void init() {
        jFrame.setBounds(300,200,500,400);
        //usernameBox.add(Box.createHorizontalStrut(80));
        usernameBox.add(usernameLabel);
        usernameBox.add(Box.createHorizontalStrut(20));
        usernameBox.add(username);
        //usernameBox.add(Box.createHorizontalStrut(80));

        //passwordBox.add(Box.createHorizontalStrut(80));
        passwordBox.add(passwordLabel);
        passwordBox.add(Box.createHorizontalStrut(20));
        passwordBox.add(password);
        //passwordBox.add(Box.createHorizontalStrut(80));

        identityGroup.add(identityButton1);
        identityGroup.add(identityButton2);

        //identityBox.add(Box.createHorizontalStrut(80));
        identityBox.add(identityLabel);
        identityBox.add(Box.createHorizontalStrut(20));
        identityBox.add(identityButton1);
        identityBox.add(identityButton2);
        //identityBox.add(Box.createHorizontalStrut(60));

        IDBox.add(IDLabeel);
        IDBox.add(Box.createHorizontalStrut(20));
        IDBox.add(ID);

        mainBox.add(info);
        mainBox.add(Box.createVerticalStrut(40));
        mainBox.add(usernameBox);
        mainBox.add(Box.createVerticalStrut(40));
        mainBox.add(passwordBox);
        mainBox.add(Box.createVerticalStrut(40));
        mainBox.add(IDBox);
        mainBox.add(Box.createVerticalStrut(40));
        mainBox.add(identityBox);
        mainBox.add(Box.createVerticalStrut(40));
        mainBox.add(button);

        mainPanel.add(mainBox);
        jFrame.add(mainPanel);

        jFrame.setVisible(true);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String newUsername = username.getText();
                String newPassword = password.getText();
                String newIdentity = null;
                String newID = ID.getText();

                if(identityButton1.isSelected()) {
                    newIdentity = "user";
                }
                else {
                    newIdentity = "manager";
                }
                if (Objects.equals(newUsername,"") || Objects.equals(newPassword,"")) {
                    JOptionPane.showMessageDialog(null,"请输入账户密码");
                }
                else {
                    Boolean isRegisted = false;
                    Connection conn = null;
                    try {
                        conn = com.util.DBUtil.getConnection();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    try {
                        isRegisted = new UserRun().addUser(conn,newUsername,newPassword,newIdentity,newID);
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                    if(isRegisted == true) {
                        JOptionPane.showMessageDialog(null,"注册成功");
                    }
                    else {
                        JOptionPane.showMessageDialog(null,"注册失败");
                    }

                }

            }
        });

    }

}
